package com.atguigu.petstore.sys.controller;

import com.atguigu.petstore.common.result.Result;
import com.atguigu.petstore.sys.minio.component.FileUploadComponent;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author lfy
 * @Description
 * @create 2023-09-23 8:58
 */
@Tag(name = "文件上传")
@RequestMapping("/api/sys")
@RestController
public class SysFileUploadController {



    @Autowired
    FileUploadComponent fileUploadComponent;


    /**
     * 前端：
     * <form action="" method="post" enctype="multipart/form-data">
     *    用户名： <input name="username" type="text"/> <br/>
     *     头像： <input name="headerImg" type="file"> <br/>
     *     宠物照： <input name="petImg" type="file" multiple> <br/>
     * 后端：
     * @RequestPart("headerImg") MultipartFile headerImg,
     * @RequestPart("petImg") MultipartFile[] petImg,
     * @RequestParam("username") String username
     * </form>
     * @return
     */
    @Operation(summary = "上传单个文件")
    @PostMapping("/file/upload")
    public Result uploadFile(@RequestPart("file") MultipartFile file) throws Exception {


        //上传文件到Minio，并返回访问路径
        String path = fileUploadComponent.upload(file);

        return Result.ok(path);
    }
}
